package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.work.keep.adapter.PlaygroundAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.PlaygroundData;
import com.work.keep.ui.PullToRefreshRecyclerView;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * http:\/\/userimg.shiwan.com\/uploads\/manage\/6d\/3f\/9d91a9bdaa4ad7fc75bb1ee8ea9e05e022ca.jpg
 * http:\/\/userimg.shiwan.com\/uploads\/manage\/6d\/3f\/9d91a9bdaa4ad7fc75bb1ee8ea9e05e022ca.jpg
 * https://api.gotokeep.com/v1.1/workouthashtag/list
 * Created by dllo on 16/1/21.
 */
public class PlaygroundActivity extends BaseActivity {
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    PullToRefreshRecyclerView pullToRefreshRecyclerView;
    private RecyclerView myRecyclerView;
    private PlaygroundAdapter playgroundAdapter;
    private GridLayoutManager gridLayoutManager;
    private String playgroundUrl = "https://api.gotokeep.com/v1.1/workouthashtag/list";

    public static void goToPlaygroundActivity(Context context) {
        Intent intent = new Intent(context, PlaygroundActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected View getLayoutView() {
        pullToRefreshRecyclerView = new PullToRefreshRecyclerView(this);
        return pullToRefreshRecyclerView;
    }

    @Override
    protected int getLayout() {
        return 0;
    }

    @Override
    protected void initView() {
        myRecyclerView = (RecyclerView) pullToRefreshRecyclerView.getRefreshableView();
    }

    @Override
    protected void initData() {
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
        playgroundAdapter = new PlaygroundAdapter(this);
        gridLayoutManager = new GridLayoutManager(this, 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 2;
                }
                return 1;
            }
        });
        myRecyclerView.setLayoutManager(gridLayoutManager);
        myRecyclerView.setAdapter(playgroundAdapter);

        GsonRequest<PlaygroundData> playgroundDataGsonRequest = new GsonRequest<PlaygroundData>(playgroundUrl, PlaygroundData.class, new Response.Listener<PlaygroundData>() {
            @Override
            public void onResponse(final PlaygroundData response) {
                playgroundAdapter.addData(response.getData().getResults());
                playgroundAdapter.setOnRecyclerItemClickListener(new OnRecyclerItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        SingleWorkoutActivity.goToSingleWorkoutActivity(PlaygroundActivity.this,response.getData().getResults().get(position).get_id());
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzM3NDU3NSwiZXhwIjoxNDU1OTY2NTc1LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.ljvV6C3JTBI8yRm6NxhNNQEDS2jXfA2B3g_aCfCePzY");
                map.put("Host", "api.gotokeep.com");
                map.put("X-X", "82241e05792e6420a945b049783997f95de556f4");
                return map;
            }
        };
        volleySingleton._addRequest(playgroundDataGsonRequest);
    }
}
